<template>
  <div class="Login">
    <div class="box">
      <h1>酒店系统管理平台</h1>
      <el-form
        ref="ruleFormRef"
        sytle="max-width:600px"
        :model="ruleForm"
        hide-required-asterisk
        class="demo-ruleForm"
        :rules="rules"
      >
        <!-- 账号 -->
        <el-form-item label="账号" prop="username">
          <el-input prefix-icon="User" v-model="ruleForm.username" placeholder="请输入账号" />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input
            :prefix-icon="icon"
            @focus="
              () => {
                icon = 'Unlock'
                showPwd = true
              }
            "
            @blur="
              () => {
                icon = 'Lock'
                showPwd = false
              }
            "
            v-model="ruleForm.password"
            :show-password="showPwd"
            type="password"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
          <el-button type="info">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/modules/user'
const ruleFormRef = ref<FormInstance>()
// 定义账号密码
const ruleForm = reactive({
  username: 'admin',
  password: '123456'
})

// 账号输入框验证
const validateuUser = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入账号'))
  } else if (value.length >= 5) {
    // callback表示效验通过可以放行
    callback()
  } else {
    callback(new Error('账号长度至少五位'))
  }
}
// 密码输入框验证
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少五位'))
  }
}
// 规则
const rules = reactive<FormRules<typeof ruleForm>>({
  username: [{ validator: validateuUser, trigger: 'blur' }],
  password: [{ validator: validatePassword, trigger: 'blur' }]
})

// 登录事件
// 引入用户pinia仓库
const userStore = useUserStore()
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      // 使用登录接口
      await userStore.login(ruleForm.username, ruleForm.password)
    } else {
      ElMessage({
        showClose: true,
        message: '账号或者密码长度不符合规定',
        type: 'error'
      })
      return false
    }
  })
}
// 设置变量来控制密码框左边图标的修改
let icon = ref('Lock')
// 控制右边密码显示，获取焦点才出现
const showPwd = ref(false)
</script>
<style scoped lang="scss">
.Login {
  width: 100vw;
  height: 100vh;
  background: linear-gradient(to bottom, #142334, #6894c7);
  //background-image: linear-gradient(to right bottom, red 80%, blue 20%);
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 400px;
    height: auto;
    color: #ffffff;
    padding: 20px;
    box-shadow: 0px 0 12px 2px #142334;
    h1 {
      text-align: center;
      margin: 20px 0;
      font-size: 25px;
    }
    .demo-ruleForm {
      :deep(.el-form-item__label) {
        color: white;
      }
      .btns {
        :deep(.el-form-item__content) {
          margin-left: 40px;
        }
      }
    }
  }
}
</style>
